/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import java.util.Hashtable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import Form.FirstActionForm;
import Objet.QueryBD;


/**
 *
 * @author zouafr
 */
public class FirstAction extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    
    QueryBD MaConnexion;
    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
        MaConnexion = new QueryBD();
        FirstActionForm FormCo = (FirstActionForm)form;
        HttpSession session = request.getSession(true);

        session.setAttribute("session", MaConnexion);
        
        Hashtable<String,String> maListe;
        Hashtable<String,String> maListe2;
        
        
        
        maListe=MaConnexion.listeEtudiants();
        if (maListe!=null){
                if(maListe.get(FormCo.getLogin())!= null)
                {                
                    if(maListe.get(FormCo.getLogin()).equalsIgnoreCase(FormCo.getMdp()))
                    {

                        request.setAttribute("co", MaConnexion);
                        session.setAttribute("monAuth", FormCo);
                        System.out.println(" before forward etudiant");
                        return mapping.findForward("etu");

                        
                    }
                }
                else{
                    maListe=MaConnexion.listeEnseignant();
                    if (maListe!=null){
                        if(maListe.get(FormCo.getLogin())!= null){
                            FormCo.setFailEns(true);
                        }    
                    }
                }
        }
                    
    
        
        
        maListe=MaConnexion.listeEnseignant();
        if (maListe!=null){
                //System.out.println(" recharche dans enseignant");
                if(maListe.get(FormCo.getLogin())!= null){
                    
                    if(maListe.get(FormCo.getLogin()).equalsIgnoreCase(FormCo.getMdp()))
                    {
                        request.setAttribute("co", MaConnexion);
                        session.setAttribute("monAuth", FormCo);
                        System.out.println(" before forward enseignant");
                        return mapping.findForward("ens");
                    }
                }
                else{
                    maListe=MaConnexion.listeEtudiants();
                    if (maListe!=null){
                        if(maListe.get(FormCo.getLogin())!= null){
                            FormCo.setFailEtu(true);
                        }    
                    }
                }
        } 
        
        maListe=MaConnexion.listeSecretaire();
        if (maListe!=null){
            
            System.out.println(" recherche dans secretaire");
                if(maListe.get(FormCo.getLogin())!=null ){
                    //System.out.println(" recherche dans secretaire 2");
                    System.out.println(maListe.get(FormCo.getLogin()));
                    System.out.println(maListe.get(FormCo.getMdp()));
                    
                    if(maListe.get(FormCo.getLogin()).equalsIgnoreCase(FormCo.getMdp()))
                    {
                        //System.out.println(" recherche dans secretaire 3");
                        request.setAttribute("co", MaConnexion);
                        request.setAttribute("monAuth", FormCo);
                        System.out.println(" before forward secretaire");
                        return mapping.findForward("sec");
                    }
                }
        
                else{
                    maListe=MaConnexion.listeSecretaire();
                    if (maListe!=null){
                        if(maListe.get(FormCo.getLogin())!= null){
                            System.out.println(" before fail");
                            FormCo.setFailEtu(true);
                            //request.setAttribute("erreur","Login ou mot de passe erroné");
                            //return mapping.findForward("erreur");
                        }    
                    }
                }
        }
        else{
            request.setAttribute("erreur","Login ou mot de passe erroné");
            return mapping.findForward("erreur");
        }
        return null;
       
    
    } 
         
}
    
    

